home *** CD-ROM | disk | FTP | other *** search
- Path: mhv.net!usenet
- From: pioli@mhv.net (Anthony Pioli)
- Newsgroups: comp.lang.c++
- Subject: help: Templates, operators, compiler
- Date: Fri, 01 Mar 1996 05:53:21 GMT
- Organization: MHVNet, the Mid Hudson Valley's Internet connection
- Message-ID: <4h5ov3$rhs@over.mhv.net>
- NNTP-Posting-Host: ulster-port10.mhv.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- Hi all,
-
- Having a little problem here with a template class...
-
- Want to compare two 'objects' in some methods, but the 'objects' may
- either be pointers to objects *or* real objects, and I want the
- template parameters to tell me which type of comparison to do. The
- pointers point to some object out in the heap that have a *constant*
- value throughout the programs excution. So, to be quick, I just
- compare the address of the objects themselves and not use the
- operators that the class *may* provide. If the user can't guarantee
- that the objects will have a constant address, I need to use the
- operators that they must then provide. Problem I'm having is that the
- compiler (Borland Turbo C++ 3.1 for Windows) is seeing two types of
- comparisons, one of the pointers themselves, and one for the
- operators, which MAY OR MAY NOT BE THERE. Thats
- the error - the parameter class does not always provide the operators.
-
-
- BTW- the objects are unique - no more than one copy of each
-
- Summary:
- compare pointers directly if no operators provided and know
- object's addresses are constant.
-
- OR
-
- if objects don't have constant addresses, use the provided
- operators.
-
- The template parameters will tell which is the case....
-
- Enough words, code time:
- --------------------------------------------------------
-
- template <class T, int use_operators >
- class NotFooAgain{
- T* data_ptr;
-
- public:
- void DoSomethingSpectacular( T* thing )
- {
- if ( use_operators == TRUE )
- ... data_ptr == thing ... // 'shallow' (?) comparison
- else
- ... *data_ptr == *thing...
- // this causes error if no == operator
- // defined for class T
-
- }
-
- }
-
-
- // instantiate using pointer comparisons..
- NotFooAgain<Bar, TRUE> a;
-
- // instantiate using user provided operator...
- NotFooAgain<Bar, FALSE> b;
-
-
- ----------------------------------------------------------
-
- And no, I can't force the user to provide those operators all the
- time.
- I also don't want to use #ifdef stuff either ( it's !user-friendly ).
-
- I hope there is a really simple obvious way to do this, in which case
- you can call me an idiot.
-
- Lost somewhere between C and C++,
- Anthony
-
-
- PS - if the syntax for the the method declaration is wrong, sorry.
- This is a small sample code snippet and I have not got much C++
- template experience. Code was only to give context to the operator
- expressions themselves.
-
-